package com.mvc.controller;

import java.io.File;

import com.common.core.BaseController;
import com.jfinal.core.JFinal;
import com.jfinal.kit.JsonKit;
import com.jfinal.upload.UploadFile;
import com.mvc.model.Version;
import com.mvc.request.ReqDelFile;
import com.mvc.request.ReturnData;
import com.mvc.service.SysService;
import com.mvc.service.impl.SysServiceImpl;

/***
 * 1.文件上传</p> 2.版本更新</p>  4.广告打开状态
 * 
 * @author Administrator
 * 
 */
public class SystemController extends BaseController {

	SysService sysService = new SysServiceImpl();

	

	// 版本更新
	public void updateAPK() {

		ReturnData<Version> data = sysService.checkVesion();
		renderJson(JsonKit.toJson(data));
	}

	// 图像上传
	public void upFile() {
		UploadFile file = getFile("file");
		ReturnData<String> data = sysService.updateFile(file);
		renderJson(JsonKit.toJson(data));
	}
	
	// 删除上传
	public void deleteFile() {
		
		ReqDelFile req = getRequestObject(ReqDelFile.class);
		
		 // 用户设置的默认上传目录
        String saveDir = JFinal.me().getConstants().getBaseUploadPath();
		File file = new File(saveDir+File.separator+ req.getImage());
		
		boolean isDel = true;
		if (file.exists() && file.isFile())
		{
			isDel =  file.delete();
		}
		renderJson(JsonKit.toJson(new ReturnData<Boolean>(isDel,isDel?200:201,"OK")));
	}
	
	
	public void loadFile()
	{
		String path = getPara("image");
		 // 用户设置的默认上传目录
        String saveDir = JFinal.me().getConstants().getBaseUploadPath();
		File file = new File(saveDir+File.separator+path);
		if (file.exists() && file.isFile() )
		{
			 renderFile(file);
			 return;
		}
		 renderNull();		
	}
	
}